﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebApp.BLL
{
    public class ExcelHelper
    {
        public static void CreateExcel(IList<object> datasource, List<ExcelCol> cols, string filename)
        {
            //生成Excel
            Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
            Aspose.Cells.Worksheet worksheet = workbook.Worksheets[0];

            //标题行
            int icol = 0;
            foreach (var headcol in cols)
            {
                worksheet.Cells[0, icol].SetStyle(getHeaderStyle(workbook));
                worksheet.Cells[0, icol].PutValue(headcol.ColName);
                icol++;
            }

            //内容
            int irow = 1;
            foreach (var item in datasource)
            {
                icol = 0;
                foreach (var col in cols)
                {
                    var prop = item.GetType().GetProperty(col.FieldName);
                    var value = prop.GetValue(item);

                    worksheet.Cells[irow, icol].SetStyle(getContentStyle(workbook));
                    worksheet.Cells[irow, icol].PutValue(value);

                    icol++;
                }

                irow++;
            }

            workbook.Save(filename, Aspose.Cells.SaveFormat.Excel97To2003);
        }

        public static Aspose.Cells.Style getHeaderStyle(Aspose.Cells.Workbook workbook)
        {
            Aspose.Cells.Style st = workbook.Styles[workbook.Styles.Add()];
            st.Font.IsBold = true;
            st.Borders[Aspose.Cells.BorderType.LeftBorder].LineStyle = Aspose.Cells.CellBorderType.Thin;
            st.Borders[Aspose.Cells.BorderType.TopBorder].LineStyle = Aspose.Cells.CellBorderType.Thin;
            st.Borders[Aspose.Cells.BorderType.RightBorder].LineStyle = Aspose.Cells.CellBorderType.Thin;
            st.Borders[Aspose.Cells.BorderType.BottomBorder].LineStyle = Aspose.Cells.CellBorderType.Thin;

            return st;
        }

        public static Aspose.Cells.Style getContentStyle(Aspose.Cells.Workbook workbook)
        {
            Aspose.Cells.Style st = workbook.Styles[workbook.Styles.Add()];
            st.Borders[Aspose.Cells.BorderType.LeftBorder].LineStyle = Aspose.Cells.CellBorderType.Thin;
            st.Borders[Aspose.Cells.BorderType.TopBorder].LineStyle = Aspose.Cells.CellBorderType.Thin;
            st.Borders[Aspose.Cells.BorderType.RightBorder].LineStyle = Aspose.Cells.CellBorderType.Thin;
            st.Borders[Aspose.Cells.BorderType.BottomBorder].LineStyle = Aspose.Cells.CellBorderType.Thin;
            
            return st;
        }
    }

    public class ExcelCol
    {
        public string ColName { get; set; }
        public string FieldName { get; set; }
        public string Options { get; set; }
        public ExcelCol(string colname, string fieldname, string options = "")
        {
            this.ColName = colname;
            this.FieldName = fieldname;
            this.Options = options;
        }
    }
}
